﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Snowdreamist.Text.UBB
{
    internal class SizeFactory : PrefixUBBElementFactory
    {
        public SizeFactory() : base(ElementPrefix) { }

        public override UBBElement Create(UBBContent ubbContent)
        {
            var matchResult = ElementRegex.Match(ubbContent.Text);
            if (!matchResult.Success)
                return null;

            if (matchResult.Groups.Count == 2)
            {
                int size = int.Parse(matchResult.Groups[1].Value);
                return new SizeElement(size);
            }
            else
            {
                int size = int.Parse(matchResult.Groups[1].Value);
                // ignore size unit at present
                return new SizeElement(size);
            }
        }

        #region private

        private readonly static string ElementPrefix = "size";

        private readonly static Regex ElementRegex = new Regex("size=([0-9]+)(pt|px)?", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

        #endregion private
    }

    public class SizeElement : UBBElement
    {
        public const string Label = "size";

        /// <summary>
        /// ctor
        /// </summary>
        public SizeElement() : this(0) { }

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="size"></param>
        public SizeElement(int size)
            : base(Label)
        {
            this.Size = size;
        }

        public int Size { get; private set; }

        public override string SerializeToUBBCode()
        {
            return Label + "=" + this.Size.ToString();
        }

        public override object Clone()
        {
            return new SizeElement(this.Size);
        }
    }
}